package com.qf.controller;

import com.qf.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
* RequestParam 参数名字和前端名字不一样 使用这个注解
*
*/
@Controller
@RequestMapping("/user")
@SessionAttributes(names = {"username","password"})
public class UserController {
    @GetMapping("/findByName")
    public String findByName(@RequestParam(value = "username",required = false,defaultValue = "jack") String name, @RequestParam(required = false,value = "a") Integer age, @DateTimeFormat(pattern = "yyyy-MM-dd") Date brithday){
        System.out.println("name = " + name);
        System.out.println("age = " + age);
        System.out.println("brithday = " + brithday);
        return "/hello";
    }
    @PostMapping("register")
    public String register(User user){
        System.out.println("user = " + user);
        return "/hello";
    }
    @GetMapping("/param01")
    public String param01(String[] args){
        System.out.println("args = " + Arrays.toString(args));
        return "/hello";
    }
    @GetMapping("/param02")
    public String param02(@RequestParam List<Integer> ids){
        System.out.println("ids = " + ids);
        return "/hello";
    }
    @PostMapping("register1")
    public String register1(@RequestParam Map user){
        System.out.println("user = " + user);
        return "/hello";
    }
    @GetMapping("/param03")
    public String param03(String name, ModelMap map){
        System.out.println("param03执行了");
        map.put("username","tom");
        System.out.println("name = " + name);

//        return "redirect:/hello.jsp";
        return "hello";
    }
    @GetMapping("/param04")
    public ModelAndView param04(ModelAndView modelAndView){
//        添加数据值刀model域
        modelAndView.addObject("username","admin");
        modelAndView.addObject("password","888");
//        设置视图
        modelAndView.setViewName("redirect:/hello.jsp");
        return modelAndView;
    }

}
